var curResume;
$(function () {
    curResume = new Resume();
    //个人简介
    bindClick('#desc', function ($this) {
        curResume = new Resume();
        var desc = $this.text().trim();
        curResume.desc = desc;
        curResume.update();
    });

    //个人信息编辑
    bindClick('.col-sm-6 > .field', function ($this) {
        curResume = new Resume();
        var name = getInfo('.field.nationality');
        var location = getInfo('.field.location');
        var phone = getInfo('.field.phone');
        var email = getInfo('.field.email');
        var birthday = getInfo('.field.dob');
        var home = getInfo('.field.address');
        curResume.info = {
            name: name,
            location: location,
            phone: phone,
            email: email,
            birthday: birthday,
            home: home
        }
        curResume.update();

        function getInfo(dom) {
            return $(dom).text().trim();
        }
    });

    //目标编辑
    bindClick('blockquote p', function ($this) {
        curResume = new Resume();
        curResume.target = $this.text().trim();
        curResume.update();
    });

    //技能编辑
    bindClick('ul.knowledge.list-unstyled > li', function ($this) {
        curResume = new Resume();
        var skills = [];
        $('.knowledge.list-unstyled li').each(function (i, o) {
            skills.push($(o).text().trim());
        });
        out(skills);
        curResume.skills = skills;
        curResume.update();
    });
    //关键属性更新
    function updateKeyProps() {
        curResume = new Resume();
        var key, val, prop;
        var props = [];
        $('.key-prop').each(function (i, o) {
            key = $(o).find('h4').text().trim();
            val = $(o).find('p').text().trim();
            prop = {
                key: key,
                value: val
            };
            props.push(prop);
        });
        curResume.keyProps = props;
        curResume.update();
    }

    //关键属性编辑[标题]
    bindClick('.key-prop > h4', function ($this) {
        updateKeyProps();
    });
    //关键属性编辑[内容]
    bindClick('.key-prop > p', function ($this) {
        updateKeyProps();
    });
    //教育经历编辑
    bindClick('.media p', function ($this) {
        curResume = new Resume();
        var educations = [];
        $('.media-body > p').each(function (i, o) {
            educations.push($(o).text().trim());
        });
        curResume.educations = educations;
        curResume.update();
    });
    //工作经验编辑
    bindClick('.list-group li.list-group-item', function ($this) {
        curResume = new Resume();
        var experiences = [];
        $('.section ul.list-group').each(function (i, o) {
            var experience = {
                tags: [],
                descriptions: []
            };
            $(o).find('li.list-group-item:not(.lables)').each(function (i, o) { //获取项目描述列表
                experience.descriptions.push($(o).html().trim());
            });
            $(o).find('li.list-group-item.lables span').each(function (i, o) { //获取项目标签列表
                experience.tags.push($(o).text().trim());
            });
            experiences.push(experience);
        });
        console.info(JSON.parse(JSON.stringify(experiences)));
        curResume.experiences = experiences;
        curResume.update();
    });
    //正文编辑
    bindClick('div.text', function ($this) {
        curResume = new Resume();
        curResume.text = $this.html().trim();
        curResume.update();
    });
});


function bindClick(dom, func) {
    $(dom).bind('click', function () {
        alertEditDialog($(this), func);
    })
}

function out(str) {
    console.info(str);
};
var isEdit = false;

function alertEditDialog($this, callback) {
    if (isEdit) {
        return;
    }
    isEdit = true;
    var html = $this.html();
    var $dialog = $('<div><textarea>' + html + '</textarea><button id="enter">确定</button><button id="cancel">取消</button></div>');
    var height = $(window).height() / 2;
    var width = $(window).width() / 2;
    var top = ($(window).height() / 2) - (height / 2);
    var left = ($(window).width() / 2) - (width / 2);
    $dialog.css({
        height: height,
        width: width,
        background: "red",
        position: "fixed",
        top: top,
        left: left,
        "z-index": 1000
    });
    var $editor = $dialog.find('textarea').css({
        height: "90%",
        width: "100%"
    });
    $dialog.find('#enter').click(function () {
        $this.html($editor.val());
        $dialog.remove();
        isEdit = false;
        callback($this);
    });
    $dialog.find('#cancel').click(function () {
        $dialog.remove();
        isEdit = false;
    });
    $('body').prepend($dialog);
}

function post(url, data) {
    data = JSON.stringify(data);
    console.info(data);
    $.ajax({
        url: url,
        type: "POST",
        data: data,
        contentType: "application/json;charset=utf8",
        success: function (data) {
            success(data);
        },
        error: function (data) {
            error(data);
        }
    });
}

function success(data) {
    location.reload(); //成功刷新页面
}

function error(data) {
    alert('错误代码:' + data);
}

function Resume() {
    this.url = '/';
    this.desc = null;
    this.info = null;
    this.target = null;
    this.skills = null;
    this.keyProps = null;
    this.educations = null;
    this.experiences = null;
    this.text = null;
    if (typeof this.update != 'function') {
        Resume.prototype.update = function () { //更新方法
            post(this.url, this);
        }
    }
}
